package com.lzb.compressmaster.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lzb.compressmaster.BaseAc;
import com.lzb.compressmaster.HomeActivity;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.databinding.ActivityPdfPreviewBinding;
import com.lzb.compressmaster.utils.PhotoPrivacyUtils;
import com.lzb.compressmaster.utils.RxUtil;

import java.io.File;

import io.reactivex.rxjava3.core.ObservableEmitter;

public class PdfPreviewActivity extends BaseAc<ActivityPdfPreviewBinding> implements View.OnClickListener {

    public static String sPath;

    @Override
    protected int onCreate() {
        return R.layout.activity_pdf_preview;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPdfPreviewBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvSave.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvSave) {//保存
            save();
        }
    }

    @Override
    protected void initData() {
        mDataBinding.pdfView.fromFile(new File(sPath)).load();
    }

    //保存
    private void save() {
        showDialog(getString(R.string.save_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        String savePath = PathUtils.getExternalDocumentsPath() + "/" + getPackageName() + "/" + FileUtils.getFileName(sPath);
                        FileUtils.copy(sPath, savePath);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_suc);
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

}